package com.naihe.anderson.sms.adapter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.naihe.anderson.sms.R;
import com.naihe.anderson.sms.activity.MakeNewMessageActivity;
import com.naihe.anderson.sms.activity.MakeNewSignatureActivity;
import com.naihe.anderson.sms.application.NhSmsApp;
import com.naihe.anderson.sms.entity.ContactUser;
import com.naihe.anderson.sms.manager.ContactMgr;


public class FriendCheckAdapter extends BaseAdapter{

    Context mContext;
    List<ContactUser> objects;
    ContactMgr contactMgr;
    Map<ContactUser, Boolean> checkMap;

    public FriendCheckAdapter( Context context, List<ContactUser> objects) {
    	contactMgr = ((NhSmsApp)context.getApplicationContext()).getContactMgr();
        mContext = context;
        this.objects = objects;
        
        setCheckMapData();
    }
    
    private void setCheckMapData(){
    	checkMap = new HashMap<ContactUser, Boolean>();
    	for (ContactUser element : objects) {
    		if(MakeNewMessageActivity.numberMap.get(element.getNumbers()) != null
    				||MakeNewSignatureActivity.numberMap.get(element.getNumbers()) != null){
    			checkMap.put(element, true);
    		} else {
    			checkMap.put(element, false);
    		}
		}
    }

    @Override
	public int getCount() {
		if(objects == null) return 0;
		return objects.size();
	}

	@Override
	public ContactUser getItem(int position) {
		if(objects == null) return null;
		return objects.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View view = null;
        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(mContext);
            view = inflater.inflate(R.layout.friend_check_item, null);
        } else {
            view = convertView;
        }
        
        ContactUser entityObj = objects.get(position);
		String currentChar = entityObj.getChineseNameCode().substring(0, 1);

		TextView head_piny = (TextView) view.findViewById(R.id.head_piny);
        TextView line_piny = (TextView) view.findViewById(R.id.line_piny);
        head_piny.setText(currentChar);
        if(position == 0){
        	head_piny.setVisibility(View.VISIBLE);
        	line_piny.setVisibility(View.VISIBLE);
        } else {
        	String previewChar = objects.get(position-1).getChineseNameCode().substring(0, 1);
        	if(previewChar.equals(currentChar)){
        		head_piny.setVisibility(View.GONE);
        		line_piny.setVisibility(View.GONE);
        	} else {
        		head_piny.setVisibility(View.VISIBLE);
        		line_piny.setVisibility(View.VISIBLE);
        	}
        }
        
        TextView name = (TextView) view.findViewById(R.id.name);
        TextView phonNumber = (TextView) view.findViewById(R.id.phone_number);
        CheckBox widgetCheckbox = (CheckBox) view.findViewById(R.id.widget_checkbox);
        widgetCheckbox.setChecked(checkMap.get(entityObj));
        ImageView headImage = (ImageView) view.findViewById(R.id.contactImage);
		
        if(entityObj.getPhotoId() > 0){
        	headImage.setBackgroundDrawable(new BitmapDrawable(contactMgr.getContactHeadImgByPhotoId(entityObj.getPhotoId())));
        } else {
        	headImage.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.default_head));
        }
        
        name.setText(entityObj.getName());
        phonNumber.setText(entityObj.getNumbers());
		
        entityObj = null;
        return view;
    }
    
    public Map<ContactUser, Boolean> getCheckIfMap() {
        return checkMap;
    }

    public void setCheckIfMap(ContactUser key,Boolean values){
    	checkMap.put(key, values);
    }
    
  //全选-全不选
    public void setCheckAll(boolean checked){
        for(Map.Entry<ContactUser, Boolean> entityMap : checkMap.entrySet())
        {
        	checkMap.put(entityMap.getKey(), checked);
        }
    }

    //反选
    public void setCheckReverse(){
        for(Map.Entry<ContactUser, Boolean> entityMap : checkMap.entrySet())
        {
        	checkMap.put(entityMap.getKey(), !entityMap.getValue());
        }
    }
}
